package com.leisai.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leisai.manager.dao.IndustryMapper;
import com.leisai.manager.dto.IndustryDTO;
import com.leisai.manager.pojo.Industry;
import com.leisai.manager.response.PageDataResult;
import com.leisai.manager.service.IndustryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 小懒虫
 * @date 2021/11/30
 */
@Service
public class IndustryServiceImpl implements IndustryService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Autowired
    private IndustryMapper industryMapper;
    
    
    @Override
    public Map<String, Object> addIndustry(Industry industry) {
        Map<String,Object> data = new HashMap();
        try {
       
            int result = industryMapper.insert(industry);
            if(result == 0){
                data.put("code",0);
                data.put("msg","新增失败！");
                logger.error("行业[新增]，结果=新增失败！");
                return data;
            }
            data.put("code",1);
            data.put("msg","新增成功！");
            data.put("industryId",industry.getIndustryId());
            logger.info("行业[新增]，结果=新增成功！");
        }catch (Exception e){
            e.printStackTrace();
            logger.error("行业[新增]异常！", e);
            return data;
        }
        return data;
    }

    @Override
    public Map<String, Object> updateIndustry(Industry industry) {
        Map<String,Object> data = new HashMap();
        try {
            int result = industryMapper.updateByPrimaryKey(industry);
            if(result == 0){
                data.put("code",0);
                data.put("msg","更新失败！");
                logger.error("行业[更新]，结果=更新失败！");
                return data;
            }
            data.put("code",1);
            data.put("msg","更新成功！");
            logger.info("行业[更新]，结果=更新成功！");
        }catch (Exception e){
            e.printStackTrace();
            logger.error("行业[更新]异常！", e);
            return data;
        }
        return data;
    }

    @Override
    public PageDataResult getIndustryList(Integer pageNum, Integer pageSize,IndustryDTO industryDTO) {
        PageDataResult pageDataResult = new PageDataResult();

        PageHelper.startPage(pageNum, pageSize);
        List<IndustryDTO> IndustryDTOS = industryMapper.getIndustryList(industryDTO);
        if(IndustryDTOS.size() != 0){
            PageInfo<IndustryDTO> pageInfo = new PageInfo<>(IndustryDTOS);
            pageDataResult.setList(IndustryDTOS);
            pageDataResult.setTotals((int) pageInfo.getTotal());
        }
        return pageDataResult;
    }

    @Override
    public Map<String, Object> del(Industry industry) {
        Map<String ,Object> data = new HashMap<>();
        try{
            int result = industryMapper.deleteByPrimaryKey(industry);
            if(result == 0){
                data.put("code",0);
                data.put("msg","删除失败");
                logger.error("删除失败");
                return data;
            }
            data.put("code",1);
            data.put("msg","删除成功");
            logger.info("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            logger.error("删除行业异常！", e);
        }
        return data;
    }

    @Override
    public Industry getIndustryById(Integer id) {
        return industryMapper.selectByPrimaryKey(id);
    }

}